#coding=utf-8
'''新浪的查询股票的接口,通过输入代码or拼音'''

from sdsource.web.utility import get_soup
from sdsource.models import Stock

url = 'http://suggest.sinajs.cn:8081/f.suggest?q=%s'

def query(data):
    '''data: 可以是完整or不完整的股票代码 or 拼音.'''
    ss = []
    
    soup = get_soup(url%data)
    ds = str(soup).split(';')[1:-2]

    for d in ds:
        m,c,n = d.split('\t')[1][:-1].split('-')
        #print n, type(n)
        m = m.encode('u8')
        c = c.encode('u8')
        #n = n.encode('u8')
        
        s = Stock(c)
        s.name = n
        s.market = m
         
        ss.append(s)
    
    return ss

def get_market(code):
    return query(code)[0].market
